<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;

/**
 * ApiController类，Api控制器
 *
 * @author yumao
 */

class ApiSystemEmailController extends Controller
{
    public $enableCsrfValidation = false; // API关闭CSRF验证
    // API入口
    public function actionIndex()
    {
     
        // 获取相关信息
        $post = Yii::$app->request->post();
        
        // 添加一个常量代表是系统邮件模块
        define('MODULE_NAME',Yii::$app->params['sites']['sys_email']['module_name']);
       
        
        // 入库用户信息
        if (empty($post['data']) || empty($post['token'])) {
            $return = [
                'response' => [
                    'code' => 10003,
                    'msg' => '数据不完整'
                ]
            ];
           
        } else {
            $site = Yii::$app->request->getQueryParam('site');
            $source = Yii::$app->request->getQueryParam('source'); // postman 测试接口参数
            if ('postman' == $source) {
                
                $token = $post['token'];
                $data = $post['data'];
            } else {
                $token = md5(Yii::$app->params['sites']['sys_email']['web_config'][$site]['apiToken'] . $post['data']);
                $data = json_decode($post['data'], true);
            }
            
        }
        
        $type = $data['type'];
        if ($type && $site) {
            if ($post['token'] != $token) {
                $return = [
                    'response' => [
                        'code' => 10001,
                        'msg' => '令牌错误'
                    ]
                ];
            } else {
               $obj = 'app\components\api\ApiSystemEmail' . ucwords($site);
               
               // App接口相关
               $api = new $obj();
               switch ($type) {
                    case '1':
                        $return = $api->emailOperation($data,1); // 
                        break;
                    default:
                        $return = [
                            'response' => [
                                'code' => 10003,
                                'msg' => '数据不完整'
                            ]
                        ];
                        break;
                   
               }
            } 
        } 
        echo json_encode($return);
    }

    // API导入增量评论数据
    public function actionUpdate()
    {
        $obj = 'app\components\api\ApiUpdate';
        $api = new $obj();
        $data = Yii::$app->request->post();
        $api->writeReview($data);
    }
}
